package com.ojoin.trade.supplyloan_core.web.dao.impl;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.supplyloan_core.web.domain.*;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlLoanRefundDao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-10-22
 * Time: 上午11:41
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TbSlLoanRefundDaoImpl extends BaseDaoImpl<TbSlLoanRefundLog> implements TbSlLoanRefundDao {
    @Override
    public void insertTbSlLoanRefund(TbSlLoanRefundLog tbslloanrefundvo) throws BusinessException {
        String mapMethodName ="insert";
        try{
            this.sqlSessionTemplate.insert(getSqlName(mapMethodName),tbslloanrefundvo);
        }catch (Exception e) {
            throw new DaoException(String.format("插入商户还款信息表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }

    }

    @Override
    public List<TbSlLoanRefundLogVo> queryTbSlLoanReFundvos(int userIdOfJoinOfDebtor,Pageable page) throws BusinessException {
        String mapMethodName="selectJoinid";
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),userIdOfJoinOfDebtor,getPageBounds(page));
            return list ;
        } catch (Exception e) {
            throw new DaoException(String.format("查询用户还款记录出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }

    }

    @Override
    public List<TbSlRepayCreditVo> queryRepayCreditvo(int userIdOfJoinOfDebtor, Pageable page) throws BusinessException {
        String mapMethodName="selectrepay";
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),userIdOfJoinOfDebtor,getPageBounds(page));
           return list ;
        } catch (Exception e) {
            throw new DaoException(String.format("查询用户已还清的贷款记录出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public List<TbSlLoanRefundVo> queryRefundVo(int userIdOfJoinOfDebtor, Pageable page) throws BusinessException {
    	return queryRefundVo(userIdOfJoinOfDebtor,null,null,page);
    }

    @Override
	public List<TbSlLoanRefundVo> queryRefundVo(int joinid, String begintime,
			String endtime, Pageable page) throws BusinessException {
    	 Map<String,String> params = new HashMap<String,String>();
    	 params.put("userIdOfJoinOfDebtor", joinid+"");
    	 params.put("begintime", begintime);
    	 params.put("endtime", endtime);
    	 
    	 String mapMethodName="selectrefundbyjoinid";
         try {
             List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),params,getPageBounds(page));
             return list ;
         } catch (Exception e) {
             throw new DaoException(String.format("查询用户还款记录出错！语句：%s",
                     getSqlName(mapMethodName)), e);
         }
	}
    

    @Override
    public Long queryTotalInterest(int idSlLoanCreditLog) throws BusinessException {
        String mapMethodName="selecttotalinterest";
        try {
           Long total_interest=this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),idSlLoanCreditLog);
            if(total_interest!=null)
            return total_interest ;
            else {
                return new Long(0);
            }
        } catch (Exception e) {
            throw new DaoException(String.format("统计贷款已还利息出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public List<TbSlLoanRefundTotalVo> queryRefundTotalVo(int userIdOfJoinOfDebtor,Pageable page) throws BusinessException {
        String mapMethodName="selecttotalrefund";
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),userIdOfJoinOfDebtor,getPageBounds(page));
            return list ;
        } catch (Exception e) {
            throw new DaoException(String.format("查询用户还款统计记录出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    
    @Override
    public List<TbSlCheckAccVo> queryCheckAccVo(String curdate, Pageable page) throws BusinessException {
        Map<String,Object> map=new HashMap();
        map.put("refundDate",curdate);
        String mapMethodName="slcheckacc";
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),map,getPageBounds(page));
            return list ;
        } catch (Exception e) {
            throw new DaoException(String.format("查询用户对账统计记录出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public List<TbSlLoanRefundVo> queryRenewedRefundForExcel(int joinid) throws BusinessException {

        String mapMethodName="selectRefundForExcel";
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),joinid);
            return list ;
        } catch (Exception e) {
            throw new DaoException(String.format("查询用户展期还款记录出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public List<SLRefundBaseInfoVo> queryRefundBaseInfoVO(String begintime, String endtime, Integer userIdOfJoinOfDebtor, Integer userIdOfJoinOfCreditor,Pageable pageable) throws BusinessException {
        Map<String,Object>  param=new HashMap<>();
        param.put("begintime",begintime);
        param.put("endtime",endtime);
        param.put("userIdOfJoinOfDebtor",userIdOfJoinOfDebtor);
        param.put("userIdOfJoinOfCreditor",userIdOfJoinOfCreditor);
        String methodmap="selectRefundBillInfo";
        try{
            return  this.sqlSessionTemplate.selectList(getSqlName(methodmap),param,getPageBounds(pageable));
        } catch (Exception e){
            throw new DaoException(String.format("查询还款对账的基本信息报错,语句：%s",getSqlName(methodmap)),e);
        }
    }

    @Override
    public List<SLRefundDetailVo> queryRefundDetailVo(String begintime, String endtime, Integer userIdOfJoinOfDebtor,Integer userIdOfJoinOfCreditor) throws BusinessException {
        Map<String,Object>  param=new HashMap<>();
        param.put("begintime",begintime);
        param.put("endtime",endtime);
        param.put("userIdOfJoinOfDebtor",userIdOfJoinOfDebtor);
        param.put("userIdOfJoinOfCreditor",userIdOfJoinOfCreditor);
        String methodmap="selectRefundDetailResultMap";
        try{
            return  this.sqlSessionTemplate.selectList(getSqlName(methodmap),param);
        } catch (Exception e){
            throw new DaoException(String.format("查询还款对账的具体信息报错,语句：%s",getSqlName(methodmap)),e);
        }
    }

    @Override
    public SLRefundBillVo queryRefundBillVo(String begintime, String endtime, Integer userIdOfJoinOfDebtor, Integer userIdOfJoinOfCreditor) throws BusinessException {
        Map<String,Object>  param=new HashMap<>();
        param.put("begintime",begintime);
        param.put("endtime",endtime);
        param.put("userIdOfJoinOfDebtor",userIdOfJoinOfDebtor);
        param.put("userIdOfJoinOfCreditor",userIdOfJoinOfCreditor);
        String methodmap="selectRefundVoInfo";
        try{
            return  this.sqlSessionTemplate.selectOne(getSqlName(methodmap),param);
        } catch (Exception e){
            throw new DaoException(String.format("查询还款对账的还款总额报错,语句：%s",getSqlName(methodmap)),e);
        }
    }
}
